package cn.ht.jywrwater.module.ht.service.villagemeterdevice;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.villagemeterdevice.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.villagemeterdevice.VillageMeterDeviceDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.villagemeterdevice.VillageMeterDeviceConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.villagemeterdevice.VillageMeterDeviceMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 大口径流量 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class VillageMeterDeviceServiceImpl implements VillageMeterDeviceService {

    @Resource
    private VillageMeterDeviceMapper villageMeterDeviceMapper;

    @Override
    public Long createVillageMeterDevice(VillageMeterDeviceCreateReqVO createReqVO) {
        // 插入
        VillageMeterDeviceDO villageMeterDevice = VillageMeterDeviceConvert.INSTANCE.convert(createReqVO);
        villageMeterDeviceMapper.insert(villageMeterDevice);
        // 返回
        return villageMeterDevice.getId();
    }

    @Override
    public void updateVillageMeterDevice(VillageMeterDeviceUpdateReqVO updateReqVO) {
        // 校验存在
        validateVillageMeterDeviceExists(updateReqVO.getId());
        // 更新
        VillageMeterDeviceDO updateObj = VillageMeterDeviceConvert.INSTANCE.convert(updateReqVO);
        villageMeterDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteVillageMeterDevice(Long id) {
        // 校验存在
        validateVillageMeterDeviceExists(id);
        // 删除
        villageMeterDeviceMapper.deleteById(id);
    }

    private void validateVillageMeterDeviceExists(Long id) {
        if (villageMeterDeviceMapper.selectById(id) == null) {
            throw exception(VILLAGE_METER_DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public VillageMeterDeviceDO getVillageMeterDevice(Long id) {
        return villageMeterDeviceMapper.selectById(id);
    }

    @Override
    public List<VillageMeterDeviceDO> getVillageMeterDeviceList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return villageMeterDeviceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<VillageMeterDeviceDO> getVillageMeterDevicePage(VillageMeterDevicePageReqVO pageReqVO) {
        return villageMeterDeviceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VillageMeterDeviceDO> getVillageMeterDeviceList(VillageMeterDeviceExportReqVO exportReqVO) {
        return villageMeterDeviceMapper.selectList(exportReqVO);
    }

}
